<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of client_obj
 *
 * @author Juraj
 */
class client_obj {

    //put your code here

    public function getAllClients($what = "*", $filter = array(), $order = '') {

        $db_obj = new db_obj();

        $where = 'WHERE 1=1';

        foreach ($filter as $key => $value) {
            if ($key == 'clients') {
                if (is_array($value)) {
                    $where_or .= " AND ( ";
                    foreach ($value as $k => $v) {
                        $where_or .= "klient_id='" . $v . "' OR ";
                    }
                    $where_or = substr($where_or, 0, strlen($where_or) - 4);
                    $where_or .= " )";
                    $where .= $where_or;
                }
            } else {
                if ($key == 'active') {
                    $where .= " AND " . $key . "='" . $value . "'";
                } elseif ($key == 'monthlyPrepaid >') {
                    $where .= " AND monthlyPrepaid>" . $value . "";
                }
            }
        }
        
        if (!empty($order))
            $order = " ORDER BY " . $order;

        $sql = "SELECT $what FROM " . $GLOBALS['tb_clients'] . " " . $where . $order;
        base_obj::debug("getAllClients sql", $sql);


        return $db_obj->getAll($sql);
    }

    public function getClient($id, $cols = array()) {
        $db_obj = new db_obj();
        $id = (int) $id;
        if (empty($id) || !is_numeric($id))
            return new error_obj('getClient empty id', __CLASS__, __FUNCTION__);

        $what = '';
        if (!empty($cols)) {
            foreach ($cols as $value) {
                $what .= $value . ',';
            }
            $what = substr($what, 0, strlen($what) - 1);
        } else {
            $what .= '*';
        }

        $sql = "SELECT $what FROM " . $GLOBALS['tb_clients'] . " WHERE klient_id='" . $id . "'";

        return $db_obj->getRow($sql);
    }
    
    public function getClientByName($name, $order = "", $limit = "") {
        $db_obj = new db_obj();
        $name = strip_tags($name);
        if (empty($name))
            return new error_obj('getClient empty $name', __CLASS__, __FUNCTION__);

        $sql = "SELECT klient_id,name FROM " . $GLOBALS['tb_clients'] . " WHERE name LIKE '" . $name . "%' $order $limit";
        //return $sql;
        return $db_obj->getAll($sql);
    }

    function getListOfClients($filter = array(), $order = array(), $limit = '', $what = '*') {
        $db_obj = new db_obj();
        $other_table = '';
        $other_string = '';
        $connector = '';
        if (!empty($order)){
            if (!empty($order['table'])){
                $other_table = ', ' . $order['table'];
                $other_string = "ORDER BY " . $order['table'] . '.' .  $order['orderBy'] . ' ' . $order['order'];
                $connector = 'AND ' . $GLOBALS['tb_clients'] . '.' . $order['connector'];
            } else {
                $other_string = "ORDER BY " . $order['orderBy'] . ' ' . $order['order'];
            }
        }
        if ($limit != '')
            $limit = ' LIMIT ' . $limit;
        if (strpos($what, 'COUNT') === false){
            $what = $GLOBALS['tb_clients'] . '.' . $what;
        }
        
        $where = 'WHERE 1=1';

        foreach ($filter as $key => $value) {
            $where .= " AND ".$GLOBALS['tb_clients']."." . $key . "='" . $value . "'";
        }

        $sql = "SELECT $what FROM " . $GLOBALS['tb_clients'] . " $other_table $where $connector $other_string $limit";
        base_obj::debug('getListOfClients sql', $sql);

        return $db_obj->getAll($sql);
    }
    
    public function getListOfClientsByFulltext($fulltext, $filter = array(), $order = array(), $limit = '', $what = '*') {
        $where = '';
        $db_obj = new db_obj();
        
        $other_table = '';
        $other_string = '';
        $connector = '';
        if (!empty($order)){
            if (!empty($order['table'])){
                $other_table = ', ' . $order['table'];
                $other_string = "ORDER BY " . $order['table'] . '.' .  $order['orderBy'] . ' ' . $order['order'];
                $connector = 'AND ' . $GLOBALS['tb_clients'] . '.' . $order['connector'];
            } else {
                $other_string = "ORDER BY " . $order['orderBy'] . ' ' . $order['order'];
            }
        }

        if (!empty($fulltext)) {
            $fulltext = strip_tags(trim($fulltext));

            $fulltext = explode(" ", $fulltext);
            $fulltextResults = array();
            foreach ($fulltext as $key => $word) {
                $sql = "SELECT " . $GLOBALS['tb_clients'] . ".klient_id 
                    FROM " . $GLOBALS['tb_clients'] . ",
                         " . $GLOBALS['tb_users'] . "    
                    WHERE 
                    " . $GLOBALS['tb_users'] . ".users_id = " . $GLOBALS['tb_clients'] . ".contactPerson AND (
                    " . $GLOBALS['tb_clients'] . ".name LIKE '%" . $word . "%' OR 
                    " . $GLOBALS['tb_clients'] . ".fullName LIKE '%" . $word . "%' OR 
                    " . $GLOBALS['tb_clients'] . ".address LIKE '%" . $word . "%' OR 
                    " . $GLOBALS['tb_clients'] . ".conditions LIKE '%" . $word . "%' OR 
                    " . $GLOBALS['tb_clients'] . ".city LIKE '%" . $word . "%' OR
                    " . $GLOBALS['tb_users'] . ".surname LIKE '%" . $word . "%' OR
                    " . $GLOBALS['tb_users'] . ".name LIKE '%" . $word . "%'
                    )";
                $fulltextResults[] = array_unique($db_obj->getCol($sql));
            }
            if (isset($fulltextResults[0]) && !empty($fulltextResults[0])) {
                $temp = $fulltextResults[0];
                foreach ($fulltextResults as $key => $array) {
                    $temp = array_intersect($temp, $array);
                }
                if (!empty($temp)) {
                    $where .= 'WHERE (';
                    foreach ($temp as $id) {
                        $where .= "klient_id='" . $id . "' OR ";
                    }
                    $where = substr($where, 0, $length - 4);
                    $where .= ')';
                } else {
                    $where .= 'WHERE 1=2';
                }
            } else {
                $where .= 'WHERE 1=2';
            }
        } else {
            $where .= 'WHERE 1=1';
        }

        if ($limit != '')
            $limit = ' LIMIT ' . $limit;
        if (strpos($what, 'COUNT') === false){
            $what = $GLOBALS['tb_clients'] . '.' . $what;
        }
        
        foreach ($filter as $key => $value) {
            $where .= " AND ".$GLOBALS['tb_clients']."." . $key . "='" . $value . "'";
        }

        $sql = "SELECT $what FROM " . $GLOBALS['tb_clients'] . " $other_table $where $connector $other_string $limit";
        base_obj::debug('getListOfClients sql', $sql);

        return $db_obj->getAll($sql);
    }

    function getClientTypeOfCooperations() {
        $db_obj = new db_obj();

        $sql = "SELECT * FROM " . $GLOBALS['tb_type_of_cooperations'] . " ORDER BY id";
        return $db_obj->getAll($sql);
    }

    function getClientTypeOfCooperation($id) {
        $db_obj = new db_obj();
        $id = (int) $id;
        if (empty($id) || !is_numeric($id))
            return new error_obj('empty id', __CLASS__, __FUNCTION__);

        $sql = "SELECT * FROM " . $GLOBALS['tb_type_of_cooperations'] . " WHERE id='" . $id . "'";
        base_obj::debug('sssql', $sql);

        return $db_obj->getRow($sql);
    }

    function checkAdd_client($check_data) {
        $result = array();
        foreach ($check_data as $key => $value) {
            switch ($key) {
                case 'name':
                    if (empty($value))
                        $result[] = $key;
                    break;
                case 'active':
                    if ($value!=0 && $value!=1)
                        $result[] = $key;
                    break;
            }
        }
        return $result;
    }
    
    public function addClient($in_data) {
        if (empty($in_data)) {
            return new error_obj('addClient -> empty $in_data', get_class(), __FUNCTION__);
        }
        $db_obj = new db_obj();
        $result = $db_obj->insert($GLOBALS['tb_clients'], $in_data);
        if (error_obj::isError($result))
            return $result;
        else
            return $db_obj->lastInsertedId();
    }
    
    public function deleteClientById($id) {
        $id = (int) $id;
        if (empty($id)){
            return new error_obj('deleteClient -> empty $id', get_class(), __FUNCTION__);
        }
        $db_obj = new db_obj();
        
        $sql = "DELETE FROM " . $GLOBALS['tb_clients'] . " WHERE klient_id='".$id."'";
        
        $result = $db_obj->query($sql);
        return $result;
    }
    
    public function editClient($in_data, $id) {
        $id = (int) $id;
        if (empty($id)) {
            return new error_obj('empty $id', get_class(), __FUNCTION__);
        }
        $db_obj = new db_obj();
        $keys = array('klient_id' => $id);
        return $db_obj->update($GLOBALS['tb_clients'], $in_data, $keys);
    }
    
    public function getClientByIP($ip) {
        $db_obj = new db_obj();
        $ip = strip_tags($ip);
        if (empty($ip))
            return new error_obj('getClient empty ip', __CLASS__, __FUNCTION__);

        $sql = "SELECT klient_id FROM " . $GLOBALS['tb_clients'] . " WHERE ip LIKE '" . $ip . "'";
        //return $sql;
        return $db_obj->getOne($sql);
    }
    
    public static function smarty_getClientName($params){
        $id = $params['id'];
        if (empty($id)){
            return '';
        } else {
            $client = client_obj::getClient($id);
            if (error_obj::isError($client))
                return '';
            return $client['name'];
        }
    }
    
    public function getClientByDomain($domain) {
        $db_obj = new db_obj();
        $domain = strip_tags($domain);
        if (empty($domain))
            return new error_obj('getClient empty domain', __CLASS__, __FUNCTION__);

        $sql = "SELECT * FROM " . $GLOBALS['tb_clients'] . " WHERE domain LIKE '" . $domain . "'";
        //return $sql;
        return $db_obj->getRow($sql);
    }

}

?>
